
import { Injectable } from '@angular/core';
import { HttpHeaders, HttpClient, HttpParams } from '@angular/common/http';
import { Observable, empty } from 'rxjs';
import { switchMapTo } from 'rxjs/operators';
import { environment } from '../environments/environment';


import { User } from '../models/User';
import { Message } from '../models/Message';

@Injectable()
export class UserService {


  private baseUrl = environment.apiUrl + 'api/user';
  private messageUrl = environment.apiUrl + 'api/sysmsg';

  constructor(
    private http: HttpClient
  ) {
  }

  getAllUsers(): Observable<{res: User[], success: boolean, total: number}> {
    return this.http.get<{res: User[], success: boolean, total: number}>(`${this.baseUrl}/getlist`);
  }

  getUsers(current: number = 1, pageSize: number = environment.pageSize, departmentId?: number): Observable<{res: User[], success: boolean, total: number}> {
    let params: HttpParams = new HttpParams();
    params = params.append('page', String(current));
    params = params.append('pageSize', String(pageSize));
    if (departmentId) {
      params = params.append('departmentId', String(departmentId));
    }
    return this.http.get<{res: User[], success: boolean, total: number}>(`${this.baseUrl}/getlist`, { params : params});
  }

  getUser(id: string): Observable<{res: User, success: boolean}> {
    return this.http.get<{res: User, success: boolean}>( `${this.baseUrl}/getuserbyid?id=${id}`);
  }

  update(user: User): Observable<{res: User, success: boolean}> {
    const url = `${this.baseUrl}/update`;
    return this.http
      .post<{res: User, success: boolean}>(url, user);
  }
  changePassword(user: User): Observable<{res: number, success: boolean}> {
    return this.http.post<{ res: number, success: boolean }>(`${this.baseUrl}/updatepassword`, user);
  }

  register(user: User): Observable<{success: boolean, res: User}> {
    return this.http
      .post<{success: boolean, res: User}>(`${this.baseUrl}/insert`, user);
  }

  isExist(username: string): Observable<{success: boolean}> {
    return this.http
      .get<{success: boolean}>(`${this.baseUrl}/checkusername?username=${username}`);
  }

  del(id: number): Observable<{success: boolean}> {
    const url = `${this.baseUrl}/del/${id}`;
    return this.http.post<{success: boolean}>(url, {});
  }

  findpassword(username: string, password: string): Observable<{success: boolean}> {
    return this.http.patch<{success: boolean}>(`${this.baseUrl}/findpassword?username=${username}&password=${password}`, { });
  }

  resetpassword(id: number): Observable<{success: boolean}> {
    return this.http.get<{success: boolean}>(`${this.baseUrl}/resetpassword?id=${id}`);
  }
  getmsgs(current: number = 1, pageSize: number = environment.pageSize, type?: string, status?: string): Observable<{res: Message[], success: boolean, total: number}> {
    let params: HttpParams = new HttpParams();
    params = params.append('page', String(current));
    params = params.append('pageSize', String(pageSize));
    if (type) {
      params = params.append('type', String(type));
    }
    if (status) {
      params = params.append('status', String(status));
    }
    return this.http.get<{res: Message[], success: boolean, total: number}>(`${this.messageUrl}/list`, { params : params});
  }
  insertmsg(message: Message): Observable<{success: boolean}> {
    return this.http.post<{success: boolean}>(`${this.messageUrl}/insert`, message);
  }
  delmsg(id: number): Observable<{success: boolean}> {
    return this.http.delete<{success: boolean}>(`${this.messageUrl}/del/${id}`);
  }
  updatestatus(id: number): Observable<{success: boolean}> {
    return this.http.get<{success: boolean}>(`${this.messageUrl}/updatestatus?id=${id}`);
  }
}

